# Replication archive for 
# Coppock, Alexander, Donald P. Green, and Ethan Porter. 2022. 
# Does Digital Advertising Affect Vote Choice? Evidence from a Randomized Field Experiment 
# Research & Politics. 

rm(list = ls())
# setwd() to replication archive

library(tidyverse)
library(estimatr)
library(texreg)

precinct_level <- read_rds("CGP_2022_precinct_level.rds")

# vote share --------------------------------------------------------------

fit_1 <-
  lm_robust(
    dem_two_party_share_2018 ~ Z,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )

fit_2 <-
  lm_robust(
    dem_two_party_share_2018 ~ Z +
      dem_two_party_share_2016_nona + vote_2016_missing +
      dem_two_party_share_2014_nona + vote_2014_missing +
      dem_two_party_share_2012_nona + vote_2012_missing,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )

fit_3 <-
  lm_robust(
    dem_two_party_share_2018 ~ treatment,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )

fit_4 <-
  lm_robust(
    dem_two_party_share_2018 ~ treatment +
      dem_two_party_share_2016_nona + vote_2016_missing +
      dem_two_party_share_2014_nona + vote_2014_missing +
      dem_two_party_share_2012_nona + vote_2012_missing,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )

list(fit_1, fit_2, fit_3, fit_4) %>% 
  map_df(tidy) %>% 
  filter(term %in% c("Z", "treatmentvideo_1", "treatmentvideo_2"))


# to check coefficient names
texreg(list(fit_1, fit_2, fit_3, fit_4),
       include.ci = FALSE,
       digits = 3)

texreg(
  list(fit_1, fit_2, fit_3, fit_4),
  custom.coef.names = c(
    "Intercept",
    "Any Treatment Video",
    "Two Party Vote Share (2016)",
    "Missingness Indicator (2016)",
    "Two Party Vote Share (2014)",
    "Missingness Indicator (2014)",
    "Two Party Vote Share (2012)",
    "Missingness Indicator (2012)",
    "Treatment Video 1",
    "Treatment Video 2"
  ),
  digits = 4,
  stars = 0.05,
  custom.note = "%stars. CR2 Cluster-robust standard errors are in parentheses.",
  reorder.coef = c(2, 9, 10, 3, 4, 5, 6, 7, 8, 1),
  include.ci = FALSE,
  include.adjrs = FALSE,
  include.rmse = FALSE,
  dcolumn = TRUE,
  booktabs = TRUE,
  use.packages = FALSE,
  table = FALSE,
  # file = "../output_for_overleaf/florida_regression_share.tex"
)

# vote margin ---------------------------------------------------

fit_1 <-
  lm_robust(
    dem_two_party_vote_margin_2018 ~ Z,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )


fit_2 <-
  lm_robust(
    dem_two_party_vote_margin_2018 ~ Z + 
      dem_two_party_vote_margin_2016_nona + vote_2016_missing +
      dem_two_party_vote_margin_2014_nona + vote_2014_missing +
      dem_two_party_vote_margin_2012_nona + vote_2012_missing,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )

fit_3 <-
  lm_robust(
    dem_two_party_vote_margin_2018 ~ treatment,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )


fit_4 <-
  lm_robust(
    dem_two_party_vote_margin_2018 ~ treatment + 
      dem_two_party_vote_margin_2016_nona + vote_2016_missing +
      dem_two_party_vote_margin_2014_nona + vote_2014_missing +
      dem_two_party_vote_margin_2012_nona + vote_2012_missing,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )

texreg(
  list(fit_1, fit_2, fit_3, fit_4),
  custom.coef.names = c(
    "Intercept",
    "Any Treatment Video",
    "Two Party Vote Margin (2016)",
    "Missingness Indicator (2016)",
    "Two Party Vote Margin (2014)",
    "Missingness Indicator (2014)",
    "Two Party Vote Margin (2012)",
    "Missingness Indicator (2012)",
    "Treatment Video 1",
    "Treatment Video 2"
  ),
  digits = 2,
  stars = 0.05,
  custom.note = "%stars. CR2 Cluster-robust standard errors are in parentheses.",
  reorder.coef = c(2, 9, 10, 3, 4, 5, 6, 7, 8, 1),
  include.ci = FALSE,
  include.adjrs = FALSE,
  include.rmse = FALSE,
  dcolumn = TRUE,
  booktabs = TRUE,
  use.packages = FALSE,
  table = FALSE,
  # file = "../output_for_overleaf/florida_regression_margin.tex"
)


# vote total ---------------------------------------------------

fit_1 <-
  lm_robust(
    vote_total_2018 ~ Z,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )


fit_2 <-
  lm_robust(
    vote_total_2018 ~ Z + 
      vote_total_2016_nona + vote_2016_missing +
      vote_total_2014_nona + vote_2014_missing +
      vote_total_2012_nona + vote_2012_missing,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )

fit_3 <-
  lm_robust(
    vote_total_2018 ~ treatment,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )


fit_4 <-
  lm_robust(
    vote_total_2018 ~ treatment + 
      vote_total_2016_nona + vote_2016_missing +
      vote_total_2014_nona + vote_2014_missing +
      vote_total_2012_nona + vote_2012_missing,
    clusters = vb_tsmart_zip,
    data = precinct_level
  )

texreg(
  list(fit_1, fit_2, fit_3, fit_4),
  custom.coef.names = c(
    "Intercept",
    "Any Treatment Video",
    "Two Party Vote Total (2016)",
    "Missingness Indicator (2016)",
    "Two Party Vote Total (2014)",
    "Missingness Indicator (2014)",
    "Two Party Vote Total (2012)",
    "Missingness Indicator (2012)",
    "Treatment Video 1",
    "Treatment Video 2"
  ),
  digits = 2,
  stars = 0.05,
  custom.note = "%stars. CR2 Cluster-robust standard errors are in parentheses.",
  reorder.coef = c(2, 9, 10, 3, 4, 5, 6, 7, 8, 1),
  include.ci = FALSE,
  include.adjrs = FALSE,
  include.rmse = FALSE,
  dcolumn = TRUE,
  booktabs = TRUE,
  use.packages = FALSE,
  table = FALSE,
  # file = "../output_for_overleaf/florida_regression_total.tex"
)

